import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { map } from "rxjs/operators";
import { Subject } from 'rxjs';

@Injectable()
export class ReportConfigService {
    private routeMsgPushSource = new Subject<any>();
    routeMsgPush$ = this.routeMsgPushSource.asObservable();
    constructor(private http: HttpClient) { }
    /**父子组件通讯 */
    announceMission(data: any) {
        this.routeMsgPushSource.next(data);
    }
    /**加载统计查询树列表 */
    loadReportTree() {
        return this.http.get('/report/api/config/loadreports').pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 加载下拉选项 */
    loadSelectItems() {
        return this.http.get('/report/api/config/items').pipe(
            map((res: Response) => res['dataMap'])
        )
    }
    /** 根据数据源加载数据库表和视图 */
    loadDataviews(dsId: number) {
        return this.http.get('/report/api/config/dataviews?dsId=' + dsId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 创建报表 */
    createReport(report: any){
        return this.http.post('/report/api/config/create', report).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 加载统计详情 */
    loadReport(reportId: number) {
        return this.http.get('/report/api/config/load?id=' + reportId).pipe(
            map((res: Response) => res['dataMap'])
        )
    }

    /** 更新统计基本信息 */
    updateReport(report: any) {
        return this.http.post('/report/api/config/update', report).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 删除统计 */
    deleteReport(reportId: number) {
        return this.http.get('/report/api/config/delete?id=' + reportId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载统计字段 */
    loadReportFields(reportId: number) {
        return this.http.get('/report/api/config/loadfields?reportId=' + reportId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载统计查询条件 */
    loadReportSearchCond(reportId: number) {
        return this.http.get('/report/api/config/cond/list?reportId=' + reportId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 创建查询条件 */
    createReportConds(conds: any) {
        return this.http.post('/report/api/config/cond/create', conds).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 删除查询条件 */
    deleteReportCond(condId) {
        return this.http.get('/report/api/config/cond/delete?condId=' + condId).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 保存报表查询条件 */
    updateReportConds(conds: any[]) {
        return this.http.post('/report/api/config/cond/update', conds).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载权限过滤配置信息 */
    listReportAuthFilter(reportId: number) {
        return this.http.get('/report/api/config/auth/list?reportId=' + reportId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 删除权限列表信息 */
    deleteReportAuthFilter(reportId: number, partId: number) {
        return this.http.get('/report/api/config/auth/delete?reportId=' + reportId + '&partId=' + partId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 保存或更新权限过滤信息 */
    updateReportAuthFilters(filters: any) {
        return this.http.post('/report/api/config/auth/saveupdate', filters).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载分组字段列表 */
    loadReportStatisGroupFields(reportId: number) {
        return this.http.get('/report/api/config/statis/group/list?reportId=' + reportId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 删除分组字段信息 */
    deleteReportStatisGroupField(id: number) {
        return this.http.get('/report/api/config/statis/group/delete?id=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 保存分组信息 */
    saveUpdateReportStatisGroupFields(fields: any[], id) {
        return this.http.post('/report/api/config/statis/group/saveupdate?id='+id, fields).pipe(
            map((res: Response) => res['data'])
        )
    }

    /** 加载分组字段列表 */
    loadReportStatisSummaryFields(reportId: number) {
        return this.http.get('/report/api/config/statis/summary/list?reportId=' + reportId).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 删除分组字段信息 */
    deleteReportStatisSummaryField(id: number) {
        return this.http.get('/report/api/config/statis/summary/delete?id=' + id).pipe(
            map((res: Response) => res['data'])
        )
    }
    /** 保存分组信息 */
    saveUpdateReportStatisSummaryFields(fields: any[], id) {
        return this.http.post('/report/api/config/statis/summary/saveupdate?id='+id, fields).pipe(
            map((res: Response) => res['data'])
        )
    }

}